package cn.zxf.note.note.type;

import cn.zxf.note.common.jpa.EntityRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import jakarta.transaction.Transactional;
import java.util.List;

public interface NoteTypeRepository extends EntityRepository<NoteType> {

    default List<NoteType> findByUserId(Integer userId) {
        return this.findByUserIdAndStatus(userId, NoteTypeStatusEnum.NORMAL.code);
    }

    @Query("FROM NoteType WHERE status = :status AND userId = :uid ORDER BY id DESC")
    List<NoteType> findByUserIdAndStatus(
            @Param("uid") Integer userId,
            @Param("status") Integer status
    );


    default int countByUserId(Integer userId) {
        return this.countByUserIdAndStatus(userId, NoteTypeStatusEnum.NORMAL.code);
    }

    @Query("SELECT COUNT(*) FROM NoteType WHERE status = :status AND userId = :uid")
    int countByUserIdAndStatus(
            @Param("uid") Integer userId,
            @Param("status") Integer status
    );


    @Deprecated
    @Transactional
    void deleteByUserId(Integer userId);

}
